import Vue from 'vue';
import { Component, Watch } from 'vue-property-decorator';
import { Dialog, Button,Loading } from 'element-ui';
import until from 'services/api/until';
import {fetchapi} from 'services/api/fetchapi';
Vue.use(Dialog)
Vue.use(Button)
Vue.use(Loading.directive);

Vue.prototype.$loading = Loading.service;


import QRCode from "qrcode"
import { app } from 'electron';
import { LoadingServiceOptions, ElLoadingComponent } from 'element-ui/types/loading';
@Component({
	components: {
		
	},
})



export default class Main extends Vue {
    
	isLogin=false;
	loading=false;
	fetchApi=new fetchapi();
	changeStudioDialog=false;
	publicList:any[]=[];
	chooseStudioId:string="wxda796dd1c06823fc";
	sureAppID:string="wxda796dd1c06823fc";
	wxname:string="云南名师"
	nowItem:any={};
	

	
	created() {

	}

	mounted() {
		const sureAppID=localStorage.getItem("app_id")
		if(sureAppID){
			this.sureAppID=sureAppID;
			this.chooseStudioId=sureAppID
			this.wxname=localStorage.getItem("wxname")
		}
		else{
			localStorage.setItem("app_id",this.sureAppID);
			localStorage.setItem("wxname",this.wxname);
		}
		const jwt_token=localStorage.getItem("jwt_token");
		if(jwt_token){
			var strings = jwt_token.split(".");
			var jsonToken = JSON.parse(decodeURIComponent(escape(window.atob(strings[1].replace(/-/g, "+").replace(/_/g, "/"))))); 
			console.log(jsonToken)
			let nowTime=Date.parse(new Date().toString())/1000;
			console.log(nowTime)
			if(nowTime<jsonToken.exp_time && nowTime-jsonToken.exp_time>-30*60){
				this.getToken();
			}
			else if(nowTime>=jsonToken.exp_time){
				setTimeout(()=>{
					this.getLoginQrcode();
					this.getPublicList();
				},1000)
			}
			else{
				
				this.isLogin=true;
				this.$emit("loginSuccess")
				
				
			}
		}
		else{
			setTimeout(()=>{
				this.getLoginQrcode();
				this.getPublicList();
			},1000)
		}
		

		
		

		
		// setTimeout(()=>{
		// 	this.getLoginQrcode();
		// 	this.getPublicList();
		// },1000)
		
	}

	async getToken(){
		let app_id = this.sureAppID;
		let srcUrl="/teaching_studio_api/user/get_token?app_id="+app_id
		const data:any=await this.fetchApi.fetchGet(srcUrl)
		if(data.errcode==0){
			localStorage.setItem("jwt_token",data.other)
			this.isLogin=true;
			this.$emit("loginSuccess")
		}
	}


	loginToken="";
	extime=0;
	async getLoginQrcode() {
		let app_id = this.sureAppID;
		this.loading=true
        let srcUrl="/api_nologin/login/wechat_qrcode/get/code?app_id="+app_id
		const data:any=await this.fetchApi.fetchGet(srcUrl)
		if(data.errcode==0){
			this.loading=false
			this.useqrcode(data.other)
			this.loginToken=data.other.token
			this.extime=data.other.extime
			this.getCodeStatus();
		}
		console.log(data)
	}
	//获取公众号
	async getPublicList(){
		let srcUrl="/api_nologin/wechat/list"
		const data:any=await this.fetchApi.fetchGet(srcUrl)
		if(data.errcode==0){
			this.publicList=data.other
		}
	}

	//选中公众号
	async chooseStudio(item:any){
		console.log(item)
		this.chooseStudioId=item.app_id;
		this.nowItem=item
	}

	//切换工作号
	surechangeStudio(item:any){

		this.changeStudioDialog=false;
		this.sureAppID=this.nowItem.app_id;
		this.wxname=this.nowItem.name

		localStorage.setItem("app_id",this.sureAppID)
		localStorage.setItem("wxname",this.wxname)
		this.getLoginQrcode();
	}
	showChangeStudioDialog(){
		this.changeStudioDialog=true;
	}


	 //生成二维码
	 useqrcode(qrcode:any) {
		var canvas = document.getElementById("canvas");
		let url = qrcode.code ;
		console.log(this)
		// 调用函数去生成二维码，参数依次为：二维码的容器、要生成的内容、回调函数    
		QRCode.toCanvas(canvas, url, function (error:any) {

			if (error) {
				console.error(error);
			} else {
				console.log("success!");
			}
		})
	}

	time_out: NodeJS.Timeout=null;
        //判断二维码是否扫描
	async getCodeStatus(){
		if(this.time_out){
			clearTimeout(this.time_out)
		}
		this.time_out=setTimeout(()=>{
			this.getCodeStatus()
		},5000)
		let app_id=this.sureAppID
		let token=this.loginToken
		let srcUrl="/api_nologin/login/wechat_qrcode/is/login?app_id="+app_id+"&token="+token
		const data:any=await this.fetchApi.fetchGet(srcUrl)
		if(data.errcode==0){
			clearTimeout(this.time_out)
			this.isLogin=true;
	
			localStorage.setItem("jwt_token",data.other)

			this.$emit("loginSuccess");
			// setTimeout(()=>{
			// 	this.getActivityList()
			// },1000)
			
			// this.useqrcode(data.other)
			// this.loginToken=data.other.token
			// this.extime=data.other.extime
			// this.getCodeStatus();
		}
	
		
    }
	//获取活动列表
	async getActivityList(){
		let app_id=this.sureAppID
		let list_type="none"
		let page=1
		let each_page=10
		let classes_id=""
		let name=""
		let srcUrl="/teaching_studio_api/activity/newest?app_id="+app_id+"&list_type="+list_type+"&page="+page+"&each_page="+each_page+"&classes_id="+classes_id+"&name="+name
		const data:any=await this.fetchApi.fetchGet(srcUrl)
		console.log(data)
	}

}
